<template>
	<div class="businessMask">
		<div ref="elememt"  :class="windowShow ? 'businessDialog large' : 'businessDialog-hide'" style="width: calc(100vw - 95px);height: calc(100vh - 98px);margin: 5px 0px 0px 5px;">
			<div class="dialog-header">
				<span>{{ titleName }}</span>
				<span class="icon iconfont iconbiaoqianlan-guanbi" @click="hide"></span>
			</div>
			<div class="dialog-body" style="height: calc(100% - 48px);padding-top: 0px;">
				<div class="buttons-wrapper">
					<div class="businessSearchCondition" style="width: 160px;">
						<label>采购类型: </label>
						<el-select class="myInput" v-model="params.type" @change="changeType">
							<el-option
									v-for="item in typeList"
									:key="item.value"
									:label="item.label"
									:value="item.value"
							></el-option>
						</el-select>
					</div>
					<div class="businessSearchCondition" style="width: 200px;">
						<label>采购方式: </label>
						<el-select class="myInput" v-model="params.way" @change="changeType">
							<el-option
									v-for="item in wayList"
									:key="item.id"
									:label="item.name"
									:value="item.id"
							></el-option>
						</el-select>
					</div>
					<div class="businessSearchCondition">
						<label>工程: </label>
						<el-input :disabled="params.type==0"  @dblclick.native="selectGc()" placeholder="请选择工程" v-model="params.gcmc"></el-input>
					</div>
					<div class="businessSearchCondition">
						<label>物资编码: </label>
						<el-input placeholder="输入物资编码" v-model="params.partsCode"></el-input>
					</div>
					<div class="businessSearchCondition">
						<label>物资名称: </label>
						<el-input placeholder="输入物资名称" v-model="params.partsName"></el-input>
					</div>
                    <div class="businessSearches-right">
                        <span class="businessButtonCancel" @click="beforeSearch()">立即查询</span>
                        <span style="margin-left: 12px;" class="businessButtonConfirm" @click="checkGcId">确认添加</span>
                    </div>
				</div>

				<div class="manage-list">
					<div class="divtable">							
						<el-table class="businessTable"
							:height="tableHeight"
					      :data="tableData"
					      style="width: 100%"
                        ref="multipleTable">
                            <el-table-column
                                    type="selection"
                                    width="55">
                            </el-table-column>
                            <el-table-column
                                    prop="gcbh"
                                    label="工程编号"
                                    width="130"
                                    :show-overflow-tooltip="true"
                                    v-if="showEngineeringInfo">
                            </el-table-column>
                            <el-table-column
                                    prop="gcmc"
                                    label="工程名称"
                                    width="130"
                                    :show-overflow-tooltip="true"
                                    v-if="showEngineeringInfo">
                            </el-table-column>
                            <el-table-column
                                    prop="code"
                                    label="采购申请编号"
                                    :show-overflow-tooltip="true"
                                    width="300px">
                            </el-table-column>
                            <el-table-column
                                    prop="partsCode"
                                    label="物资编码"
                                    :show-overflow-tooltip="true">
                            </el-table-column>
                            <el-table-column
                                    prop="partsName"
                                    label="物资名称"
                                    :show-overflow-tooltip="true">
                            </el-table-column>
                            <el-table-column
                                    prop="partsType"
                                    label="规格型号"
                                    :show-overflow-tooltip="true"
                                    width="130px">
                            </el-table-column>
                            <el-table-column
                                    prop="partsUnit"
                                    label="单位"
                                    width="50px">
                            </el-table-column>
                            <el-table-column
                                    prop="applyNum"
                                    label="申请数量"
                                    width="130"
                                    class-name="applyNumStyle">
                                <template slot-scope="scope">
                                    <el-input-number :step="1" v-model="scope.row.applyNum" @change="handleNumChange" :min="0" :max="tableDataCopy[scope.$index].applyNum"  size="mini"></el-input-number>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    prop="typeName"
                                    label="采购类型"
                                    :show-overflow-tooltip="true">
                            </el-table-column>
                            <el-table-column
                                    prop="wayName"
                                    label="采购方式"
                                    :show-overflow-tooltip="true">
                            </el-table-column>
					   </el-table>
					</div>
                    <div class="dialog-header" style="background-color: #fff">
                        <span>订货信息</span>
                    </div>
                    <el-row>
                        <div class="buttons-wrapper">
                            <div class=" businessSearches-left">
                                <div class="businessSearchCondition">
                                    <label>工程名称: </label>
                                    <el-input placeholder="输入工程名称" :disabled="true" v-model="formData.gcmc"></el-input>
                                </div>
                                <div class="businessSearchCondition">
                                    <label>供应商: </label>
                                    <el-input @dblclick.native="selectSupplier()" placeholder="供应商" v-model="formData.supplierName"></el-input>
                                </div>
                                <div class="businessSearchCondition">
                                    <label>采购类型: </label>
                                    <el-input placeholder="采购类型" :disabled="true" v-model="typeList[formData.type+1].label"></el-input>
                                </div>
                                <div class="businessSearchCondition">
                                    <label>采购方式: </label>
                                    <el-input placeholder="采购方式" :disabled="true" v-model="formData.wayString"></el-input>
                                </div>
                                <div class="businessSearchCondition" style="width: 300px;">
                                    <label>备注: </label>
                                    <el-input placeholder="输入备注" v-model="formData.remark"></el-input>
                                </div>
                            </div>
                            <div class="businessSearches-right">
                                <span style="margin-left: 12px;" class="businessButtonConfirm" @click="submitFormdata()">保存</span>
                            </div>
                        </div>
                        <el-col :span="15">
                            <div class="divtable">
                                <el-table class="businessTable"
                                          :height="tableHeight"
                                          :data="wzList"
                                          style="width: 100%"
                                            @row-click="showDetailList">
                                    <el-table-column
                                            type="selection"
                                            width="55">
                                    </el-table-column>
                                    <el-table-column
                                            prop="partsCode"
                                            label="物资编码"
                                            :show-overflow-tooltip="true">
                                    </el-table-column>
                                    <el-table-column
                                            prop="partsName"
                                            label="物资名称"
                                            :show-overflow-tooltip="true">
                                    </el-table-column>
                                    <el-table-column
                                            prop="partsType"
                                            label="规格型号"
                                            :show-overflow-tooltip="true">
                                    </el-table-column>
                                    <el-table-column
                                            prop="partsUnit"
                                            label="单位">
                                    </el-table-column>
                                    <el-table-column
                                            prop="num"
                                            label="订货数量"
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="price"
                                            label="单价"
                                            :show-overflow-tooltip="true"
                                            width="130"
                                            class-name="applyNumStyle">
                                        <template slot-scope="scope">
                                            <el-input-number :step="0.01" step-strictly v-model="scope.row.price" :min="0.01" size="mini"></el-input-number>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="操作">
                                        <template slot-scope="scope">
                                            <i class="el-icon-delete" @click.stop="removeDetail(scope.$index, scope.row)"></i>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </div>
                        </el-col>
                        <el-col :span="8" :offset="1">
                            <div class="divtable" >
                                <el-table class="businessTable"
                                          :height="tableHeight"
                                          :data="detailList"
                                          style="width: 100%">
                                    <el-table-column
                                            type="selection"
                                            width="55">
                                    </el-table-column>
                                    <el-table-column
                                            type="index"
                                            label="序号"
                                            :show-overflow-tooltip="true">
                                    </el-table-column>
                                    <el-table-column
                                            prop="code"
                                            label="采购申请编号"
                                            :show-overflow-tooltip="true">
                                    </el-table-column>
                                    <el-table-column
                                            prop="orderNum"
                                            label="订货数量"
                                    >
                                    </el-table-column>
                                    <el-table-column label="操作">
                                        <template slot-scope="scope">
                                            <i class="el-icon-delete" @click.stop="removeWzDetail(scope.$index, scope.row)"></i>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </div>
                        </el-col>
                    </el-row>
				</div>
			</div>
		</div>
        <!-- 工程选择弹窗 -->
        <template-window
                v-if="engineeringWindowShow"
                :params="engineeringWindowParams"
                ref="engineeringWindow"
                @confirm="getWindowData"
                @cancel="handelEngineeringWindow"
        ></template-window>
        <!-- 供应商弹窗 -->
        <template-window
                v-if="supplierWindowShow"
                :params="supplierWindowParams"
                ref="supplierWindow"
                @confirm="getSupplierWindowData"
                @cancel="handelSupplierWindow"
        ></template-window>
	</div>
</template>
<script>
    import TemplateWindow from "@/system/pages/customPage/templateWindow";
	import { wayListFromDict,
        getAppleDetailList,
        savePartsOrder} from "@/business/api/partsOrder.js";
	export default {
		name: "partsOrder",
        components:{
            TemplateWindow
        },
		props: {
			titleName:{
				default:"新增订货"
			},
            relatedWinParams:{
            }
		},
		data() {
			return {
				windowShow:true,
				params:{
                    type:-1,
                    way:-1,
                    start:0,
                    limit:0,
                    gcbh:"",
                    gcmc:"",
                    partsCode:"",
                    partsName:"",
                    code:"",
                    gcid:""
				},
				totalCount: 0,
      			pageIndex: 1,
      			pageSize: 10,	
      			originPageSize:null,
      			tableData: [],
                tableDataCopy:[],
		        tableHeight:200,
                typeList:[{
                    value:-1,
                    label:"全部"
                },{
				    value:0,
					label:"库存"
				},{
                    value:1,
                    label:"一收一付"
                }],
                wayList:[{
                    id:-1,
                    name:"全部"
                }],
                applyNumStyle:{
				    padding:"none !important"
                },
                wzList:[],
                selectGcId:0,
                detailList:[],
                selectDetailIndex:-1,
                haveChanged:false,
                formData:{
                    funcId:0,
                    gcId:0,
                    type:0,
                    way:0,
                    supplier:0,
                    remark:"",
                    fillDeptId:0,
                    partsOrderSubList:[],
                    supplierName:"",
                    wayString:""
                },
                engineeringWindowShow: false,
                engineeringWindowParams: {
                    id: "683", // 页面 id
                    //menuCode: "JY_PS_GCXX", // 页面编码
                    multipleChoice: "0", // 0-单选 1-多选
                    dialogTitle: "工程管理", // 标题
                    deptId: 0, // 部门 ID
                    cellParams: {}, // 按钮传给表单参数
                    businessInfo: {}, // 按钮传给表单后台参数
                    serverParams: {}, // 按钮传给后台参数
                    params:{
                        userId:0
                    }
                },
                userInfo:{},
                supplierWindowShow: false,
                supplierWindowParams: {
                    id: "1283", // 页面 id
                    menuCode: "JY_PS_GYSGL", // 页面编码
                    multipleChoice: "0", // 0-单选 1-多选
                    dialogTitle: "供应商管理", // 标题
                    deptId: 0, // 部门 ID
                    cellParams: {}, // 按钮传给表单参数
                    businessInfo: {}, // 按钮传给表单后台参数
                    serverParams: {}, // 按钮传给后台参数
                    params:{
                        "wf_state":"2"
                    }
                },
                showEngineeringInfo:false
			};
		},
		created:function(){
		    this.getWayList(581,-1);
		    this.search();
		    this.formData.fillDeptId = this.relatedWinParams.deptId;
		    this.formData.funcId = this.relatedWinParams.funcId;
		    this.engineeringWindowParams.deptId = this.relatedWinParams.deptId;
            this.supplierWindowParams.deptId = this.relatedWinParams.deptId;
            this.userInfo = this.$store.state.user.info;
		},
		mounted: function() {
			this.originPageSize = this.pageSize;
		},
		methods: {
            //显示工程选择弹框
            selectGc(){
                if(this.params.type==1){
                    this.engineeringWindowParams.params.userId =  this.userInfo.id;
                    this.engineeringWindowShow = true;
                }
            },
            //显示供应商选择弹框
            selectSupplier(){
                this.supplierWindowShow = true;
            },
			hide(){
				this.windowShow = false;
				setTimeout(() => {
                    this.$emit("close");
				}, 400);
			},
			handleCurrentChange(e) {
		      this.pageIndex = e;
		      this.search();
		    },
		    // 每页显示页码
		    handleSizeChange(e) {
		      this.pageSize = e;
		      this.search();
		    },
            beforeSearch(){

               // if(this.haveChanged && this.wzList.length>0){
               //     this.messageBox("切换采购类型或者采购方式会清空已选物资，确认搜索吗？").then((res) => {
               //         if (res.action) {
               //             this.search();
               //             this.haveChanged = false;
               //             this.wzList = [];
               //             if(this.params.type == 1){
               //                 this.showEngineeringInfo = true;
               //             }else{
               //                 this.showEngineeringInfo = false;
               //                 this.params.gcid = 0;
               //                 this.params.gcmc = "";
               //             }
               //         }
               //     });
               // }else{
			    //    if(this.params.type == 1 && this.params.gcid == 0){
			    //        this.message("请选择工程",3)
               //     }else{
               //         if(this.params.type == 1){
               //             this.showEngineeringInfo = true;
               //         }else{
               //             this.showEngineeringInfo = false;
               //             this.params.gcid = 0;
               //             this.params.gcmc = "";
               //         }
               //         this.search();
               //     }
               // }
                this.search();
            },
		    search(){
                this.haveChanged = false;
                this.tableData = [];
                this.tableDataCopy = [];
                let params = this.params;
                if(params.type==-1){
                    params.type=null;
                }
                if(params.way==-1){
                    params.way=null;
                }
                getAppleDetailList(params).then((res) => {
					if (res.success) {
					    let info = res.info;
                   		 if(info.length>0){
                   		     //如果原来有选中数据，则查数据时，减去相同数据的数量
                   		     if(this.wzList.length>0){
                                 for(let i=0;i< info.length;i++){
                                     for(let j=0;j< this.wzList.length;j++){
                                         if(info[i].partsId == this.wzList[j].partsId){
                                             for(let k = 0 ; k < this.wzList[j].partsApplyOrderList.length;k++){
                                                 if(info[i].applyId == this.wzList[j].partsApplyOrderList[k].applyId && info[i].applySubId == this.wzList[j].partsApplyOrderList[k].applySubId){
                                                     info[i].applyNum -= this.wzList[j].partsApplyOrderList[k].orderNum;
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 this.tableData = info;
                                 if(info.length>0){
                                     for(let i=0;i<res.info.length;i++){
                                         this.tableDataCopy.push({
                                             applyNum:res.info[i].applyNum,
                                             detailId:res.info[i].applySubId
                                         })
                                     }
                                 }
                             }else{
                                 this.tableData = info;
                                 if(info.length>0){
                                     for(let i=0;i<res.info.length;i++){
                                         this.tableDataCopy.push({
                                             applyNum:res.info[i].applyNum,
                                             detailId:res.info[i].applySubId
                                         })
                                     }
                                 }
                             }
                         }
					} else this.message(res.msg, 3);
				});
		    },
			getWayList(id,type){
                wayListFromDict({ pid: id }).then((res) => {
					if (res.success) {
                    		//this.wayList = res.info;
							if(res.info && res.info.length>0){
                                for(let i=0;i<res.info.length;i++){
                                    this.wayList.push(res.info[i])
                                }
								//this.params.way = this.wayList[0].id;
                            }
							if(type == 0 ){
							    this.search();
                            }
						} else this.message(res.msg, 3);
					});
				},
            handleNumChange(value){

            },
            checkGcId(){
                let selection = this.$refs.multipleTable.selection;
                let wayList = '';
                let typeList = '';
                let gcList = '';
                if(selection.length>0){
                    for(let i = 0 ; i < selection.length;i++){
                        if(i==0){
                            wayList += selection[i].wayName;
                            typeList += selection[i].typeName;
                            gcList += selection[i].gcmc;
                            if(selection.length == 1){
                                //判断已选数据
                                if(this.wzList.length==0){
                                    //设置选择的工程
                                    this.formData.gcId = selection[0].gcId;
                                    this.formData.gcmc = selection[0].gcmc;
                                    //设置采购方式
                                    for(let i=0;i<this.wayList.length;i++){
                                        if(this.wayList[i].name == selection[0].wayName){
                                            this.formData.way = this.wayList[i].id;
                                        }
                                    }
                                    this.formData.wayString = selection[0].wayName;
                                    //设置采购类型
                                    this.formData.type = selection[0].typeName=='一收一付'?1:0;
                                    this.toggleSelection();
                                }else{
                                    if(this.typeList[this.formData.type+1].label!=selection[0].typeName){
                                        this.message('请选择相同的采购类型', 3);
                                    }else if (selection[0].wayName!=this.formData.wayString){
                                        this.message('请选择相同的采购方式', 3);
                                    }else if(this.formData.gcId != selection[0].gcId){
                                        this.message('请选择相同的工程', 3);
                                    }else{
                                        //设置选择的工程
                                        this.formData.gcId = selection[0].gcId;
                                        this.formData.gcmc = selection[0].gcmc;
                                        //设置采购方式
                                        for(let i=0;i<this.wayList.length;i++){
                                            if(this.wayList[i].name == selection[0].wayName){
                                                this.formData.way = this.wayList[i].id;
                                            }
                                        }
                                        this.formData.wayString = selection[0].wayName;
                                        //设置采购类型
                                        this.formData.type = selection[0].typeName=='一收一付'?1:0;
                                        this.toggleSelection();
                                    }
                                }
                            }
                        }else{
                            if( wayList.indexOf(selection[i].wayName) == -1 ){
                                this.message('请选择相同的采购方式', 3);
                                return;
                            }else if( typeList.indexOf(selection[i].typeName) == -1 ){
                                this.message('请选择相同的采购类型', 3);
                                return;
                            }else if( gcList.indexOf(selection[i].gcmc) == -1 ){
                                this.message('请选择相同的工程', 3);
                                return;
                            }else if(i==selection.length-1){
                                if(this.wzList.length == 0){
                                    //设置选择的工程
                                    this.formData.gcId = selection[0].gcId;
                                    this.formData.gcmc = selection[0].gcmc;
                                    //设置采购方式
                                    for(let i=0;i<this.wayList.length;i++){
                                        if(this.wayList[i].name == selection[0].wayName){
                                            this.formData.way = this.wayList[i].id;
                                        }
                                    }
                                    this.formData.wayString = selection[0].wayName;
                                    //设置采购类型
                                    this.formData.type = selection[0].typeName=='一收一付'?1:0;
                                    this.toggleSelection();
                                }else{
                                    if(this.typeList[this.formData.type+1].label!=selection[0].typeName){
                                        this.message('请选择相同的采购类型', 3);
                                    }else if (selection[0].wayName!=this.formData.wayString){
                                        this.message('请选择相同的采购方式', 3);
                                    }else if(this.formData.gcId != selection[0].gcId){
                                        this.message('请选择相同的工程', 3);
                                    }else{
                                        //设置选择的工程
                                        this.formData.gcId = selection[0].gcId;
                                        this.formData.gcmc = selection[0].gcmc;
                                        //设置采购方式
                                        for(let i=0;i<this.wayList.length;i++){
                                            if(this.wayList[i].name == selection[0].wayName){
                                                this.formData.way = this.wayList[i].id;
                                            }
                                        }
                                        this.formData.wayString = selection[0].wayName;
                                        //设置采购类型
                                        this.formData.type = selection[0].typeName=='一收一付'?1:0;
                                        this.toggleSelection();
                                    }
                                }
                            }
                        }
                        //this.toggleSelection();
                    }
                }
                // if(this.params.type == 1){
                //     let selection = this.$refs.multipleTable.selection;
                //     if(selection.length > 0 ){
                //         let flag = false;
                //         for(let i = 0 ; i < selection.length; i ++){
                //             if(this.wzList.length == 0 && this.selectGcId == 0 ){
                //                 this.selectGcId = selection[0].gcId;
                //             } else if(selection[i].gcId != this.selectGcId){
                //                 this.message('不能选择多个工程的物资', 3);
                //                 flag = true;
                //                 break;
                //             }
                //         }
                //         if(!flag){
                //             //设置选择的工程
                //             this.formData.gcId = selection[0].gcId;
                //             this.formData.gcmc = selection[0].gcmc;
                //             this.toggleSelection();
                //         }
                //     }
                // }else{
                //     this.toggleSelection();
                // }
            },
            toggleSelection(){
                let wzList = [];
                let selection = this.$refs.multipleTable.selection;
                //如果之前没有选中的物资，则将现在选中的物资存入list
			    if(this.wzList.length == 0){
                    for( let i = 0 ; i < selection.length; i++){
                        if(selection[i].applyNum != 0 ){
                            //判断是否有一样的物资
                            if(wzList.length>0){
                                let flag = false;
                                for( let j = 0 ; j < wzList.length; j++){
                                    if(wzList[j].partsId == selection[i].partsId){
                                        //如果有，则叠加
                                        wzList[j].num += selection[i].applyNum;
                                        let detailFlag = false;
                                        if( wzList[j].partsApplyOrderList.length > 0){
                                            for(let k=0;k< wzList[j].partsApplyOrderList.length;k++){
                                                if(wzList[j].partsApplyOrderList[k].applyId == selection[i].applyId && wzList[j].partsApplyOrderList[k].partsId == selection[i].partsId&& wzList[j].partsApplyOrderList[k].applySubId == selection[i].applySubId){
                                                    detailFlag = true;
                                                    wzList[j].partsApplyOrderList[k].orderNum += selection[i].applyNum;
                                                }
                                            }
                                        }
                                        if(!detailFlag){
                                            wzList[j].partsApplyOrderList.push({
                                                //采购申请id
                                                applyId:selection[i].applyId,
                                                //采购申请明细id
                                                applySubId:selection[i].applySubId,
                                                //物料id
                                                partsId:selection[i].partsId,
                                                //申请订货数量
                                                orderNum:selection[i].applyNum,
                                                code:selection[i].code
                                            })
                                        }
                                        flag = true;
                                        this.changeMaxNum(selection[i].applySubId,selection[i].applyNum);
                                        break;
                                    }
                                }
                                if(!flag){
                                    //如果没有，则新增
                                    let wzListDetail = {
                                        //物料id
                                        partsId:selection[i].partsId,
                                        //单价
                                        price:1.00,
                                        //订货数量
                                        num:selection[i].applyNum,
                                        partsCode:selection[i].partsCode,
                                        partsName:selection[i].partsName,
                                        partsType:selection[i].partsType,
                                        partsUnit:selection[i].partsUnit
                                    }
                                    wzListDetail.partsApplyOrderList=[{
                                        //采购申请id
                                        applyId:selection[i].applyId,
                                        //采购申请明细id
                                        applySubId:selection[i].applySubId,
                                        //物料id
                                        partsId:selection[i].partsId,
                                        //申请订货数量
                                        orderNum:selection[i].applyNum,
                                        code:selection[i].code
                                    }]
                                    wzList.push(wzListDetail)
                                    this.changeMaxNum(selection[i].applySubId,selection[i].applyNum);
                                }
                            } else {
                                //如果没有，则新增
                                let wzListDetail = {
                                    //物料id
                                    partsId:selection[i].partsId,
                                    //单价
                                    price:1.00,
                                    //订货数量
                                    num:selection[i].applyNum,
                                    partsCode:selection[i].partsCode,
                                    partsName:selection[i].partsName,
                                    partsType:selection[i].partsType,
                                    partsUnit:selection[i].partsUnit
                                }
                                wzListDetail.partsApplyOrderList = [{
                                    //采购申请id
                                    applyId:selection[i].applyId,
                                    //采购申请明细id
                                    applySubId:selection[i].applySubId,
                                    //物料id
                                    partsId:selection[i].partsId,
                                    //申请订货数量
                                    orderNum:selection[i].applyNum,
                                    code:selection[i].code
                                }]
                                this.changeMaxNum(selection[i].applySubId,selection[i].applyNum);
                                wzList.push(wzListDetail)
                            }
                        }
                    }
                }else{
                    wzList = this.wzList;
			        let selection = this.$refs.multipleTable.selection;
			        for( let i = 0 ; i < selection.length; i++){
                        if(selection[i].applyNum != 0 ){
                            let flag = false;
                            for( let j = 0 ; j < wzList.length; j++){
                                //如果是同一个物资，则累加
                                if(selection[i].partsId == wzList[j].partsId){
                                    wzList[j].num += selection[i].applyNum;
                                    let detailFlag = false;
                                    if( wzList[j].partsApplyOrderList.length > 0){
                                        for(let k=0;k< wzList[j].partsApplyOrderList.length;k++){
                                            if(wzList[j].partsApplyOrderList[k].applyId == selection[i].applyId && wzList[j].partsApplyOrderList[k].partsId == selection[i].partsId&& wzList[j].partsApplyOrderList[k].applySubId == selection[i].applySubId){
                                                detailFlag = true;
                                                wzList[j].partsApplyOrderList[k].orderNum += selection[i].applyNum;
                                            }
                                        }
                                    }
                                    if(!detailFlag){
                                        wzList[j].partsApplyOrderList.push({
                                            //采购申请id
                                            applyId:selection[i].applyId,
                                            //采购申请明细id
                                            applySubId:selection[i].applySubId,
                                            //物料id
                                            partsId:selection[i].partsId,
                                            //申请订货数量
                                            orderNum:selection[i].applyNum,
                                            code:selection[i].code
                                        })
                                    }

                                    flag = true;
                                    this.changeMaxNum(selection[i].applySubId,selection[i].applyNum);
                                    break;
                                }
                            }
                            if(!flag){
                                //如果没有，则新增
                                let wzListDetail = {
                                    //物料id
                                    partsId:selection[i].partsId,
                                    //单价
                                    price:1.00,
                                    //订货数量
                                    num:selection[i].applyNum,
                                    partsCode:selection[i].partsCode,
                                    partsName:selection[i].partsName,
                                    partsType:selection[i].partsType,
                                    partsUnit:selection[i].partsUnit
                                }
                                wzListDetail.partsApplyOrderList = [{
                                    //采购申请id
                                    applyId:selection[i].applyId,
                                    //采购申请明细id
                                    applySubId:selection[i].applySubId,
                                    //物料id
                                    partsId:selection[i].partsId,
                                    //申请订货数量
                                    orderNum:selection[i].applyNum,
                                    code:selection[i].code
                                }]
                                this.changeMaxNum(selection[i].applySubId,selection[i].applyNum);
                                wzList.push(wzListDetail)
                            }
                        }
                    }
                }
                this.wzList = wzList;
            },
            changeMaxNum(val,dNum){
                //修改最大可选数量
                for(let j=0 ; j< this.tableDataCopy.length;j++){
                    if(this.tableDataCopy[j].detailId == val){
                        this.tableDataCopy[j].applyNum -= dNum ;
                        this.tableData[j].applyNum = this.tableDataCopy[j].applyNum ;
                    }
                }
            },
            showDetailList(row, column, event){
                this.selectDetailIndex = column;
                this.detailList = row.partsApplyOrderList;
            },
            removeDetail(index,row){
                //移除已选数据，将上方可选最大值加回去
                this.wzList.splice(index,1);
                for(let i = 0; i < row.partsApplyOrderList.length;i++){
                    for(let j = 0 ; j < this.tableDataCopy.length;j++){
                        if(row.partsApplyOrderList[i].applySubId == this.tableDataCopy[j].detailId){
                            this.tableDataCopy[j].applyNum += row.partsApplyOrderList[i].orderNum;
                        }
                    }
                    for(let j = 0 ; j < this.tableData.length;j++){
                        if(row.partsApplyOrderList[i].applySubId == this.tableData[j].applySubId){
                            this.tableData[j].applyNum += row.partsApplyOrderList[i].orderNum;
                        }
                    }
                }
                //隐藏右侧列表
                this.selectDetailIndex = -1;
                this.detailList = [];
            },
            removeWzDetail(index,row){
                for(let j = 0 ; j < this.tableDataCopy.length;j++){
                    if(row.applySubId == this.tableDataCopy[j].detailId){
                        this.tableDataCopy[j].applyNum += row.orderNum;
                    }
                }
                for(let j = 0 ; j < this.tableData.length;j++){
                    if(row.applySubId == this.tableData[j].applySubId){
                        this.tableData[j].applyNum += row.orderNum;
                    }
                }
                //移除已选数据，将上方可选最大值加回去
                for(let i = 0; i < this.wzList.length;i++){
                    for(let j = 0 ; j < this.wzList[i].partsApplyOrderList.length;j++){
                        if(this.wzList[i].partsApplyOrderList[j] != undefined &&this.wzList[i].partsApplyOrderList[j].applyId == row.applyId && this.wzList[i].partsApplyOrderList[j].applySubId == row.applySubId && this.wzList[i].partsApplyOrderList[j].code == row.code ){
                            console.log('---')
                            this.wzList[i].partsApplyOrderList.splice(index,1);
                            this.wzList[i].num -= row.orderNum;
                            if(this.wzList[i].num==0){
                                this.wzList.splice(i,1)
                            }
                            return
                        }
                    }
                }
            },
            changeType(val){
			    this.haveChanged = true;
            },
            getWindowData(e){
                this.params.gcid = e[0].GC_ID;
                this.params.gcmc = e[0].GCMC;
                this.engineeringWindowShow = false;
            },
            handelEngineeringWindow(){
                this.engineeringWindowShow = false;
            },
            getSupplierWindowData(e){
                this.formData.supplier = e[0].FBS_ID;
                this.formData.supplierName = e[0].FBSMC;
                this.supplierWindowShow = false;
            },
            handelSupplierWindow(){
                this.supplierWindowShow = false;
            },
            submitFormdata(){
               //判断数据
                if(this.formData.supplier==0){
                    this.message("请选择供应商",3);
                    return;
                } else if(this.wzList.length == 0){
                    this.message("请选择物资",3);
                    return;
                }
                //设置deptId
                //this.formData.fillDeptId = this.$store.state.user.info.deptId;
                //提交数据
                this.formData.partsOrderSubList = this.wzList
                savePartsOrder(JSON.stringify(this.formData)).then((res) => {
                    if (res.success) {
                    this.message(res.msg, 1);
                    this.hide();
                } else this.message(res.msg, 3);
            });

            }
		},

	};
</script>
<style scoped>
	.manage-list {
		height: calc(100% - 72px);
		border-radius: 6px;
		display: flex;
		flex-direction: column;
		border-radius: 6px;
	}
	.businessSearchCondition{
		width: 150px;
		margin-right: 10px;
	}
	
	.divtable{
		width: 100%;
		height: calc(50% - 24px);
	}
    .applyNumStyle .cell{
        height: auto !important;
    }
	
	
</style>
<style>
    .businessTable .el-table__body tr .applyNumStyle .cell{
        height: auto !important;
    }
</style>